package portal.conf;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import portal.common.MyStringUtils;
import portal.conf.entity.BaseConf;
import portal.conf.entity.ProcessEntity;

@Component
@Scope("singleton")
public class ProcessConf extends AbstractConf{
	public ProcessConf(){
		if(IsDebug)
			setFileName("//app//unischeduler//sbin//linux-x86_64//process.conf");
	}

	@Override 
	@Value("${conf.hostfile}")  
	public void setFileName(String val){
		super.setFileName(val);
	}
	@Override
	protected BaseConf analysiConf(String tempString, int line, Class<?> t, BaseConf qe) {
		if(MyStringUtils.isEmpty(tempString))
			return null;
		
		Method method;
		try {
			qe=(BaseConf) t.newInstance();
			qe.setLineEnd(line);
			qe.setLineStart(line);
			method = Class.forName(t.getName()).getMethod("setValue",String.class);
			method.invoke(qe,MyStringUtils.trimAll(tempString));
			addToConfList(qe);
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		return null;
	}
	protected boolean isEntityExist(String bConf) {
		if(bConf==null)return false;
		selectAllConf();
		// TODO Auto-generated method stub
		if(null!=findConfByName(bConf))
			return true;
		return false;
	}
	@Override
	protected boolean isConfStart(String line) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected boolean isConfEnd(String line) {
		// TODO Auto-generated method stub
		return true;
	}
	public boolean insertConf(String conf){
		if(conf==null)
			return false;
		if(isEntityExist(conf)){ 
			//logger.error("exists !");
			//System.out.println("exits");
			return false;
		};
		insertConf(fileName, conf.split(";"));
		selectAllConf(new File(getFileName()), ProcessEntity.class);
		return true;
	}
	public boolean removeConf(String conf){
		if(conf==null)return false;
		//System.out.println("exits1");
		selectAllConf();
		if(removeConf(findConfByName(conf))){
			selectAllConf(new File(getFileName()), ProcessEntity.class);
			return true;
		}
		return false;
	}
	public List<BaseConf> selectAllConf(){
		return selectAllConf(ProcessEntity.class);
	}
	//private Vector<String> process=new Vector<>();
	public static void main(String[] args){
		ProcessConf pc=new ProcessConf();
		
		pc.insertConf("111");
		pc.removeConf("111222");
		List<BaseConf>list =pc.selectAllConf();
		for(int index=0;index<list.size();index++){
			System.out.println(list.get(index));
		}
	}
}
